var appData = getApp().globalData
Page({
    data:{
        name:appData.nickName,
        subNum:appData.UserData.zan,
        state:'退出登录',
        userHead:'',
        editing:false,
        male:'',
        female:'',
        loading:false,
        hidden:true,
        modalText:'',
        oldname:appData.nickName,
        oldsex:appData.gender,
        sex:appData.gender
    },
    onLoad:function(){
        this.setData({
            name:appData.nickName,
            subNum:appData.UserData.zan,
            male:appData.gender==1?'act':'',
            female:appData.gender!=1?'act':'',
            sex:appData.gender,
            topNum:appData.UserData.max_heart,
            userHead:appData.avatarUrl?appData.avatarUrl:'../../asset/img/headdef.png'
        })
    },
    tapmale(){
        this.setData({
            male:'act',female:'',sex:1
        })
    },tapfemale(){
this.setData({
            male:'',female:'act',sex:2
        })
    },edittap(){
        // this.setData({
        //     editing:true
        // })
    },save(){
        let self = this;
        if(self.data.oldnmae!=self.data.name){
            self.setData({loading:true},function(){
                getApp().request({
                    url:'set_nick_name',
                    data:{
                        id:getApp().globalData.UserData.id,
                        nick_name:self.data.name
                    },success(res){
                        if(!res.data.flag){
                            self.setData({
                                hidden:false,
                                modalText:res.data.msg
                            })
                        }else{
                            self.setData({editing:false})
                        }
                    },fail(res){
                        self.setData({
                            hidden:false,
                            modalText:"请求失败"
                        })
                    },complete(){
                        self.setData({loading:false,editing:false})
                    }

                })
            })
            
        }
        if(self.data.oldsex!=self.data.sex){
            self.setData({loading:true})
            getApp().request({
                url:'set_sex',
                data:{
                    id:getApp().globalData.UserData.id,
                    sex:self.data.sex
                },success(res){
                    if(res.data.flag){
                        self.setData({editing:false})
                    }else{
                        self.setData({
                            hidden:false,
                            modalText:res.data.msg
                        })
                    }
                },fail(res){
                    self.setData({
                        hidden:false,
                        modalText:"请求失败"
                    })
                },complete(){
                    self.setData({loading:false})
                }

            })
        }
    },editname(e){
        this.setData({
            name:e.detail.value
        })
    },confirm(){
        this.setData({hidden:true})
    },exit(){
        let self = this;
        this.setData({loading:true})
        getApp().request({
            url:'login_out',data:{},success(res){
                if(res.data.flag){
                  try {
                    wx.removeStorageSync('localaccount')
                    wx.removeStorageSync('localpsw')
                    wx.removeStorageSync('localname')
                    wx.removeStorageSync('localicon')
                  } catch (e) {
                    // Do something when catch error
                  }
                  try {
                    wx.reLaunch({
                      url: '../accountlogin/login',
                    })
                  } catch (e) {
                    wx.redirectTo({
                      url: '../accountlogin/login',
                    })
                  }
                }else{
                     self.setData({
                        hidden:false,
                        modalText:res.date.msg
                    })
                }
            },fail(){},complete(){
                self.setData({loading:false})
            }
        })
    }
})